package cn.snmp.mutual.operator;

import cn.snmp.mutual.adapter.AbstractMibNodeAdapter;
import cn.snmp.mutual.adapter.SnmpAdapter;
import cn.snmp.mutual.nms.SnmpVar;

import cn.snmp.mutual.data.ObjService;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dengkn
 * SNMP 发送数据处理入口，数据转换，数据结果获取
 */
@Slf4j
public class SendSnmp4jOperator {


    /**
     * 返回错误码成功
     */
    public static String ERROR_CODE_SUCCESS = "0";
    /**
     * 返回错误码未知，配置数据错误
     */
    public static String ERROR_CODE_UNKNOW = "-2";

    /**
     * @param data 传入的ne 信息+ 下发数据 + instance
     * @return objservice 携带下发数据及返回结果
     */
    public static ObjService setInfo(ObjService data) {

        log.debug("Set device data :" + data);
        ObjService dataClone = data.clone();
        ObjService rowSet = dataClone.objectAt("RowSet", 0);
        ObjService result = new SnmpAdapter().setInfo(dataClone);
        if (ERROR_CODE_SUCCESS.equals(result.getStringValue("ErrCode"))) {
            log.error("This operation is very successful.");
        } else if (ERROR_CODE_UNKNOW.equals(result.getStringValue("ErrCode"))) {
            ObjService setDtatClone = data.clone();
            if (rowSet != null) {
                setDtatClone.setValue("Instance", rowSet.getValue("Instance"));
            }
            ObjService getFirtSetData = getInfo(setDtatClone);
            ObjService getFirtSetDataRowSet = getFirtSetData.objectAt("RowSet", 0);
            if (getFirtSetDataRowSet != null && !"".equals(getFirtSetDataRowSet)) {
                return getFirtSetData;
            }
        }
        log.debug(result.toString() + "Once Snmp Operator End:");
        return result;
    }

    /**
     * @param data 传入的ne 信息+ 下发数据 + 获取单条instance 获取多条不需要配置instance
     * @return objservice 携带下发数据及返回结果
     */
    public static ObjService getInfo(ObjService data) {

        log.debug("Once Snmp Operator Start: Get device data :" + data);
        ObjService dataClone = data.clone();
        ObjService result = new SnmpAdapter().getInfo(dataClone);
        //String instance = (String) dataClone.objectAt("RowSet", 0).getValue("Instance");
        log.debug("mutual operator result:" + result.toString());
        if (ERROR_CODE_SUCCESS.equals(result.getStringValue("ErrCode"))) {
            log.error("This operation is very successful.");
        }
        log.debug("This operation is a bit unexpected. Once Snmp Operator End");
        return result;
    }


    public static ObjService findMibNode(ObjService mibTable, String nodeLogicName) {
        if (mibTable == null) {
            return null;
        } else {
            for (int i = 0; i < mibTable.rowSize("MibNode"); ++i) {
                ObjService mibNode = mibTable.objectAt("MibNode", i);
                String logic = mibNode.getStringValue("Logic");
                if (logic.equals(nodeLogicName)) {
                    return mibNode;
                }
            }
            return null;
        }
    }

    public static SnmpVar writeSnmpVar(Object value, ObjService mibnode, ObjService option, ObjService rowSet) {
        AbstractMibNodeAdapter adapter = AbstractMibNodeAdapter.getMibNodeAdapter(mibnode, option);
        adapter.setRowSet(rowSet);
        return adapter.nativeWriteMibData(value, mibnode);
    }

}
